<template>
	<div>
		<!-- 面包屑 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>商品管理</el-breadcrumb-item>
			<el-breadcrumb-item>商品分类</el-breadcrumb-item>
		</el-breadcrumb>

		<el-card>
			<el-row>
				<el-col>
					<el-button type="primary" size="mini" @click="addCate">添加分类</el-button>
				</el-col>
			</el-row>

			<tree-table class="treeTable" :data="cateList" :columns="columns" :selection-type="false"
				:expand-type="false" show-index index-text="#" border :show-row-hover="false">
				<!-- 是否有效 -->
				<template slot="isDeleted" slot-scope="scope">
					<i v-if="!scope.row.cat_deleted" class="el-icon-success" style="color: lightgreen;"></i>
					<i v-else class="el-icon-error" style="color: red;"></i>
				</template>
				<!-- 排序 -->
				<template slot="Sort" slot-scope="scope">
					<el-tag size="mini" type="success" v-if="scope.row.cat_level===0">一级</el-tag>
					<el-tag size="mini" type="warning" v-else-if="scope.row.cat_level===1">二级</el-tag>
					<el-tag size="mini" v-else-if="scope.row.cat_level===2">三级</el-tag>
				</template>
				<!-- 操作 -->
				<template slot="Operation" slot-scope="scope">
					<el-button size="mini" type="primary" icon="el-icon-edit" @click="CateEdit(scope.row)">编辑
					</el-button>
					<el-button size="mini" type="danger" icon="el-icon-delete" @click="CateDelete(scope.row.cat_id)">删除
					</el-button>
				</template>
			</tree-table>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
				layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
		<el-dialog title="添加分类" :visible.sync="addCateDialog" width="50%" @close="ClearCate">
			<el-form ref="addCateRef" :model="addCateForm" :rules="addCateRules">
				<el-form-item label="类别名称" prop="cat_name" label-width="100px">
					<el-input autocomplete="off" v-model="addCateForm.cat_name"></el-input>
				</el-form-item>
				<el-form-item label="父级类别" label-width="100px">
					<!-- options 数据源 -->
					<el-cascader class="CateCascader" change-on-select clearable expand-trigger="hover"
						v-model="selectedCateIds" :options="parentCateList" :props="CascaderCateProp"
						@change="CatehandleChange"></el-cascader>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="addCateDialog = false">取 消</el-button>
				<el-button type="primary" @click="addCateDo">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog title="编辑分类" :visible.sync="editCateDialog" width="50%">
			<el-form ref="editCateRef" :model="editCateForm" :rules="addCateRules">
				<el-form-item label="类别名称" prop="cat_name" label-width="100px">
					<el-input autocomplete="off" v-model="editCateForm.cat_name"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="editCateDialog = false">取 消</el-button>
				<el-button type="primary" @click="editCateDo">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				queryInfo: {
					type: 3,
					pagenum: 1,
					pagesize: 5

				},
				cateList: [],
				total: 0,
				addCateDialog: false,
				editCateDialog: false,
				addCateForm: {
					cat_name: '',
					cat_pid: 0,
					cat_level: 0
				},
				addCateRules: {
					cat_name: [{
							required: true,
							message: '请输入活动名称',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 5,
							message: '长度在 3 到 5 个字符',
							trigger: 'blur'
						}
					],
				},
				parentCateList: {},
				CascaderCateProp: {
					label: 'cat_name',
					value: 'cat_id',
					children: 'children'
				},
				selectedCateIds: [],
				editCateForm: {
					cat_id: '',
					cat_name: ''
				},
				columns: [{
						label: '分类名称',
						prop: 'cat_name'
					},
					{
						label: '是否有效',
						prop: 'cat_deleted',
						minWidth: '200px',
						type: 'template',
						template: 'isDeleted'
					},
					{
						label: '排序',
						prop: 'cat_level',
						minWidth: '200px',
						type: 'template',
						template: 'Sort'
					},
					{
						label: '操作',
						minWidth: '200px',
						type: 'template',
						template: 'Operation'
					},
				]
			}
		},
		methods: {
			async getCateList() {
				const {
					data: res
				} = await this.$http.get('categories', {
					params: this.queryInfo
				});
				if (res.meta.status != 200) {
					return this.$message.error("获取分类失败");
				}
				this.cateList = res.data.result;
				this.total = res.data.total;
			},
			handleSizeChange(newSize) {
				this.queryInfo.pagesize = newSize;
				this.getCateList();
			},
			handleCurrentChange(newPageNum) {
				this.queryInfo.pagenum = newPageNum;
				this.getCateList();
			},
			// 添加分类
			addCate() {
				this.getParentCateList();
				this.addCateDialog = true;
			},
			// 获取下拉选择框的数据
			async getParentCateList() {
				const {
					data: res
				} = await this.$http.get('categories', {
					params: {
						type: 2
					}
				});
				if (res.meta.status != 200) {
					return this.$message.error("获取分类失败");
				}
				this.parentCateList = res.data;
			},
			// 级联选择器变化监听
			CatehandleChange() {
				if (this.selectedCateIds.length > 0) {
					this.addCateForm.cat_pid = this.selectedCateIds[this.selectedCateIds.length - 1];
					this.addCateForm.cat_level = this.selectedCateIds.length;
					return;
				} else {
					this.addCateForm.cat_pid = 0;
					this.addCateForm.cat_level = 0;
				}

			},
			addCateDo() {
				this.$refs.addCateRef.validate(async (valid) => {
					if (!valid) return;
					const {
						data: res
					} = await this.$http.post('categories', this.addCateForm);
					console.log(res);
					if (res.meta.status != 201) {
						return this.$message.error("添加分类失败");
					}
					this.$message.success("添加分类成功")
					this.getCateList();
					this.addCateDialog = false;
				});
			},
			ClearCate() {
				this.$refs.addCateRef.resetFields();
				this.selectedCateIds = [];
				this.addCateForm.cat_pid = 0;
				this.addCateForm.cat_level = 0;
			},
			CateDelete(id) {
				this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(async () => {
						const {
							data: res
						} = await this.$http.delete('categories/' + id);
						if (res.meta.status !== 200) {
							return this.$message.error('删除分类失败');
						}
						this.$message.success('删除分类成功');
						this.getCateList();
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						});
					});
			},
			CateEdit(data) {
				this.editCateForm.cat_id = data.cat_id;
				this.editCateForm.cat_name = data.cat_name;
				this.editCateDialog = true;
			},
			editCateDo() {
				this.$refs.editCateRef.validate(async (valid) => {
					if (!valid) return;
					const {
						data: res
					} = await this.$http.put('categories/' + this.editCateForm.cat_id, {
						cat_name: this.editCateForm.cat_name
					});
					if (res.meta.status != 200) {
						return this.$message.error("更新分类失败");
					}
					this.$message.success("更新分类成功")
					this.getCateList();
					this.editCateDialog = false;
				});
			}
		},
		created() {
			this.getCateList();
		}
	}
</script>
<style scoped>
	.treeTable {
		margin-top: 15px;
	}

	.CateCascader {
		width: 100%;
	}
</style>
